package vi.org.rongcloud19test.adapter;

import android.app.DownloadManager;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.text.SpannableString;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import io.rong.imlib.model.Message;
import io.rong.message.TextMessage;
import vi.org.rongcloud19test.R;
import vi.org.rongcloud19test.utils.Constants;
import vi.org.rongcloud19test.utils.emoji.EmojiParser;
import vi.org.rongcloud19test.utils.emoji.ParseEmojiMsgUtil;

/**
 * Created by Vicky on 2015/11/20.
 * RongCloud19Test
 * contact way: 317461087@qq.com
 */
public class ConversationAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Message> messageList;
    private LayoutInflater layoutInflater;
    private ViewHolder viewHolder;
    private String portrait;
    private Bitmap bitmap;
    private Bitmap myBitmap;

    public ConversationAdapter(Context context,ArrayList<Message> messageList,String portrait)
    {
        this.context = context;
        this.messageList = messageList;
        layoutInflater = LayoutInflater.from(context);
        this.portrait = portrait;
    }

    @Override
    public int getCount() {
        if(null!=messageList) {
            return messageList.size();
        }else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        if(null!=messageList) {
            return messageList.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        String unicode = EmojiParser.getInstance(context).parseEmoji(((TextMessage) messageList.get(i).getContent()).getContent());
        SpannableString spannableString = ParseEmojiMsgUtil.getExpressionString(context, unicode);
//        if(view == null)
//        {
        if(view == null)
        {
            Log.i("uri", portrait);
            Log.i("my uri",Constants.loginUser.getPortrait());
            bitmap = getHttpBitmap(portrait);
            myBitmap = getHttpBitmap(Constants.loginUser.getPortrait());
        }
        viewHolder = new ViewHolder();
        if(messageList.get(i).getMessageDirection().equals(Message.MessageDirection.SEND))
        {
            view = layoutInflater.inflate(R.layout.activity_conversation_to,null);
            viewHolder.content = (TextView)view.findViewById(R.id.content);
            viewHolder.user = (ImageView)view.findViewById(R.id.user_uri);
            viewHolder.content.setBackgroundResource(R.drawable.icon_jw_chatting_to_bg_nor);
            viewHolder.content.setText(spannableString);
            viewHolder.user.setImageBitmap(myBitmap);
        }
        else {
            view = layoutInflater.inflate(R.layout.activity_conversation_from,null);
            viewHolder.contentLeft = (TextView)view.findViewById(R.id.content_left);
            viewHolder.userLeft = (ImageView)view.findViewById(R.id.user_uri_left);
            viewHolder.contentLeft.setBackgroundResource(R.drawable.icon_jw_chatting_from_bg_nor);
            viewHolder.contentLeft.setText(spannableString);
            viewHolder.userLeft.setImageBitmap(bitmap);
        }
//            view.setTag(viewHolder);
//        }
//        else {
//            viewHolder = (ViewHolder)view.getTag();
            /*LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT);
            if(messageList.get(i).getMessageDirection().equals(Message.MessageDirection.SEND))
            {
                //此处相当于布局文件中的Android:layout_gravity属性
                layoutParams.gravity = Gravity.RIGHT;
                layoutParams.setMargins(10, 5, 10, 5);
                viewHolder.content.setLayoutParams(layoutParams);
                viewHolder.content.setBackgroundResource(R.drawable.icon_jw_chatting_to_bg_nor);
//                viewHolder.content.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
                //此处相当于布局文件中的Android：gravity属性
                //button.setGravity(Gravity.CENTER);
            }
            else {
                //此处相当于布局文件中的Android:layout_gravity属性
                layoutParams.gravity = Gravity.LEFT;
                layoutParams.setMargins(10,5,10,5);
                viewHolder.content.setLayoutParams(layoutParams);
                viewHolder.content.setBackgroundResource(R.drawable.icon_jw_chatting_from_bg_nor);
//                viewHolder.content.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
            }*/
//        }
//        Log.i("ConversationAdapter",((TextMessage)messageList.get(i).getContent()).getContent());

        return view;
    }
    class ViewHolder{
        TextView content;
        ImageView user;
        TextView contentLeft;
        ImageView userLeft;
    }

    /**
     * 获取网落图片资源
     * @param url
     * @return
     */
    public static Bitmap getHttpBitmap(String url){
        URL myFileURL;
        Bitmap bitmap=null;
        try{
            myFileURL = new URL(url);
            //获得连接
            HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();
            //设置超时时间为6000毫秒，conn.setConnectionTiem(0);表示没有时间限制
            conn.setConnectTimeout(5000);
            //连接设置获得数据流
            conn.setDoInput(true);
            //不使用缓存
            conn.setUseCaches(false);
            //这句可有可无，没有影响
            //conn.connect();
            //得到数据流
            InputStream is = conn.getInputStream();
            //解析得到图片
            bitmap = BitmapFactory.decodeStream(is);
            //关闭数据流
            is.close();
        }catch(Exception e){
            e.printStackTrace();
        }

        return bitmap;

    }
}
